package resource;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;


import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

import Objects.Statistic;

public class SaveGame {
	public RecordStore rs;
    
	private static boolean isSAVE = false;
    private ByteArrayOutputStream bos;
    private DataOutputStream dos;
    private ByteArrayInputStream bis;
    private DataInputStream dis;
    
    private byte[] writeByte;
    
    private static final String SAVE_STORE = "TaiXiu";
    
    public void open() {
        try {
            rs = RecordStore.openRecordStore(SAVE_STORE, true);
            if(rs.getNumRecords() == 0) {
            	//Statistic p = new Statistic();
                try {
                	bos = new ByteArrayOutputStream();
                    dos = new DataOutputStream(bos);
					dos.writeUTF("");
					dos.writeInt(0);
	                dos.writeInt(0);
	                dos.writeInt(0);
	                dos.writeInt(0);
	                dos.writeInt(0);
	                dos.writeInt(0);
	                dos.writeInt(0);
	                dos.flush();
	                
	                writeByte = bos.toByteArray();
	                rs.addRecord(writeByte, 0, writeByte.length);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        }
        catch(RecordStoreException re) {
            System.out.println("Can't open SaveGame");
            System.out.println(re.getMessage());
        }
    }
    public void close() {
        try {
            rs.closeRecordStore();
        }
        catch(NullPointerException npe) {
            System.out.println("SaveGame 's reference is null");
            System.out.println(npe.getMessage());
        }
        catch(RecordStoreException e) {
            System.out.println("Can't close SaveGame");
            System.out.println(e.getMessage());
        }
    }
    
    //clear saved game
    public void clear(){
    	isSAVE = false;
    }
    public void save(final Statistic p) {
        try {
            bos = new ByteArrayOutputStream();
            dos = new DataOutputStream(bos);
                    
            dos.writeUTF( p.getName( ) );
            dos.writeInt(p.getBankerCapital());
            dos.writeInt(p.getGamerCapital());
            dos.writeInt(p.getBigCount());
            dos.writeInt(p.getSmallCount());
            dos.writeInt(p.getWinCount());
            dos.writeInt(p.getLoseCount());
            dos.writeInt(p.getBet());
            dos.flush();
            
            writeByte = bos.toByteArray();
            rs.setRecord(1, writeByte, 0, writeByte.length);
            isSAVE = true;
        }
        catch(IOException ioe) {
            System.out.println("Can't write data to SaveGame");
            System.out.println(ioe.getMessage());
        }
        catch(RecordStoreException rno) {
            System.out.println("Can't wirte data to SaveGame becase SaveGame closing");
            System.out.println(rno.getMessage());
        }
    }
    
    public Statistic load() {
        Statistic p = new Statistic();
        try {
        	bis = new ByteArrayInputStream(rs.getRecord(1));
            dis = new DataInputStream(bis);
            
            p.setName(dis.readUTF());
           	p.setBankerCapital(dis.readInt());
            p.setGamerCapital(dis.readInt());
            p.setBigCount(dis.readInt());
            p.setSmallCount(dis.readInt());
            p.setWinCount(dis.readInt());
            p.setLoseCount(dis.readInt());
            p.setBet(dis.readInt());
        }
        catch(IOException ioe) {
            System.out.println("Can't read data from SaveGame");
            System.out.println(ioe.getMessage());
        }
        catch(RecordStoreException rno) {
            System.out.println("Can't read data to SaveGame becase SaveGame closing");
            System.out.println(rno.getMessage());
        }
        
        return p;
    }
    
    public boolean isSaved(){
    	return isSAVE;
    }
}
